#Include <GuiButton.au3>
#Include <GuiComboBox.au3>
#Include <GuiListBox.au3>
#Include <..\include\Common_For_PageManager.au3>
#Include <Excel.au3>


Global $Dll1 = "C:\Dll\Ism.dll"
Global $Dll2 = "C:\Dll\ImageOperate.dll"
Global $Dll3 = "C:\Dll\FioAll32.dll"
Global $Dll4 = "C:\Dll\Fiotif32.dll"
Global $sFileName = "Scan.Log"
Global $sFilePatch = "C:\AutomationTest\Scan\"
Global $ScanSettingsdialog = "Scan Settings"
Global $DoubleTxt_Path = "C:\Scan\DoubleByte.txt"
Global $profile = "Save As"
Global $open = "Open"
Global $sFolderName = "Scan"
Global $sPerformResultPath = 'C:\AutomationTest\Scan\Perform Result\'
Global $sCorrectResultPath = 'C:\AutomationTest\Scan\Correct Result\'
Global $sFunctionFolderPath = 'C:\AutomationTest\Scan\'
Global $sSampleFolderPath = 'C:\AutomationTest\Scan\Sample\'
Global $sExcelSheetName = 'Scan'
Global $sChrckSavedScanProfile = 'Please check saved scan profile.'
Global $sProfileExetension = '.ScnPro'

Global $sLongFolderName = '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
Dim Const $iNumber = Common_GetNumberInExcelSheet($oExcel,$sExcelSheetName) 
If $sExcelObject == False Then 
	MsgBox('','','Function for Scan --- Common_GetNumberInExcelSheet($oExcel,$sExcelSheetName)')
	Exit
EndIf
Dim Const $iCurrentSheetTotalLineNumber = $iNumber[0]

Func ImageTypeValidByDll($sColorMode)
	If $sColorMode == 'Black and White' Then
		Return 'BlackWhite'
	EndIf
	If $sColorMode == 'Gray' Then
		Return 'Gray'
	EndIf
	If $sColorMode == 'Color' Then
		Return 'Color'
	EndIf
EndFunc  
	
Func ImageHightValidByDll($sScansize)
	If $sScansize == 'A5 148 x 210 mm' Then
		Return '205'
	EndIf
	If $sScansize == 'Letter 8.5 x 11 inches' Then
		Return '820'
	EndIf
	If $sScansize == 'A4 210 x 297 mm' Then
		Return '14032'
	EndIf
	If $sScansize == 'B5 182 x 257 mm' Then
		Return '752'
	EndIf
	If $sScansize == 'Custom(Maximum*Minimum)' Then
		Return '548'
	EndIf
	If $sScansize == 'Custom(Minimum*Maximum)' Then
		Return '548'
	EndIf
	If $sScansize == '3 x 5 inches (Portrait)' Then
		Return '500'
	EndIf
	If $sScansize == '3 x 5 inches (Landscape)' Then
		Return '298'
	EndIf
	If $sScansize == '4 x 6 inches (Portrait)' Then
		Return '446'
	EndIf
	If $sScansize == '4 x 6 inches (Landscape)' Then
		Return '496'
	EndIf
	If $sScansize == '5 x 7 inches (Portrait)' Then
		Return '88'
	EndIf
	If $sScansize == '5 x 7 inches (Landscape)' Then
		Return '320'
	EndIf
	If $sScansize == 'Custom(Minimum*Minimum)' Then
		Return ''
	EndIf
	If $sScansize == 'Custom(Maximum*Maximum)' Then
		Return ''
	EndIf
	If $sScansize == '3.6 x 2.1 inches' Then
		Return ''
	EndIf
	If $sScansize == '2.1 x 3.6 inches' Then
		Return ''
	EndIf
EndFunc	

Func ImageWidthValidByDll($sScansize)
	If $sScansize == 'A5 148 x 210 mm' Then
		Return '360'
	EndIf
	If $sScansize == 'Letter 8.5 x 11 inches' Then
		Return '944'
	EndIf
	If $sScansize == 'A4 210 x 297 mm' Then
		Return '9921'
	EndIf
	If $sScansize == 'B5 182 x 257 mm' Then
		Return '2112'
	EndIf
	If $sScansize == 'Custom(Maximum*Minimum)' Then
		Return '632'
	EndIf
	If $sScansize == 'Custom(Minimum*Maximum)' Then
		Return '632'
	EndIf
	If $sScansize == '3 x 5 inches (Portrait)' Then
		Return '300'
	EndIf
	If $sScansize == '3 x 5 inches (Landscape)' Then
		Return '496'
	EndIf
	If $sScansize == '4 x 6 inches (Portrait)' Then
		Return '690'
	EndIf
	If $sScansize == '4 x 6 inches (Landscape)' Then
		Return '1224'
	EndIf
	If $sScansize == '5 x 7 inches (Portrait)' Then
		Return '203'
	EndIf
	If $sScansize == '5 x 7 inches (Landscape)' Then
		Return '408'
	EndIf
	If $sScansize == 'Custom(Minimum*Minimum)' Then
		Return ''
	EndIf
	If $sScansize == 'Custom(Maximum*Maximum)' Then
		Return ''
	EndIf
	If $sScansize == '3.6 x 2.1 inches' Then
		Return ''
	EndIf
	If $sScansize == '2.1 x 3.6 inches' Then
		Return ''
	EndIf
EndFunc	

Func ImageDPIValidByDll($iDPI)
	If $iDPI == '100  dpi' Then
		Return '100'
	EndIf
	If $iDPI == '200  dpi' Then
		Return '200'
	EndIf
	If $iDPI == '300  dpi' Then
		Return '300'
	EndIf
	If $iDPI == '400  dpi' Then
		Return '400'
	EndIf
	If $iDPI == '500  dpi' Then
		Return '500'
	EndIf
	If $iDPI == '600  dpi' Then
		Return '600'
	EndIf
	If $iDPI == '700  dpi' Then
		Return '700'
	EndIf
	If $iDPI == '800  dpi' Then
		Return '800'
	EndIf
	If $iDPI == '900  dpi' Then
		Return '900'
	EndIf
	If $iDPI == '1000  dpi' Then
		Return '1000'
	EndIf
	If $iDPI == '1200 dpi' Then
		Return '1200'
	EndIf
EndFunc	

Func TotalTestCaseNumber($sExcelSheetName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iRow = 2, $iCurrentNumber = _ExcelReadCell($oExcel, $iRow , 1), $iTotalNumber
	If $iCurrentNumber <> '' Then
		While $iCurrentNumber <> ''
			$iRow = $iRow + 1 
			$iCurrentNumber = _ExcelReadCell($oExcel, $iRow , 1)
			If $iCurrentNumber <> '' Then 
				$iTotalNumber = $iCurrentNumber
			Else
				ExitLoop
			EndIf	
		WEnd
	EndIf	
	If $iTotalNumber == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'TotalTestCaseNumber($sExcelSheetName)',$sExcelSheetName, 'Scan', ' ')
	EndIf
	Return $iTotalNumber
EndFunc

Func GetDocumentTypeFromExcel($sExcelSheetName, $iRow,$TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 2	
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetDocumentTypeFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func GetColorModeFromExcel($sExcelSheetName, $iRow,$TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 3	
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetColorModeFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func GetScanSizeFromExcel($sExcelSheetName, $iRow, $TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 4	
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetScanSizeFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func GetDPIFromExcel($sExcelSheetName, $iRow, $TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 5	
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetDPIFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func GetSaveAsTypeFromExcel($sExcelSheetName, $iRow, $TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 6
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetSaveAsTypeFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func GetEnableDocumentFeederStatusFromExcel($sExcelSheetName, $iRow, $TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 7
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetSaveAsTypeFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func GetDecreenStatusFromExcel($sExcelSheetName, $iRow,$TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 9
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetSaveAsTypeFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func GetAutocropStatusFromExcel($sExcelSheetName, $iRow, $TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 10
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetSaveAsTypeFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func GetContinueScanStatusFromExcel($sExcelSheetName, $iRow, $TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 11
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetSaveAsTypeFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func SetDocumentType($sFormat,$TestCaseID, $sFunctionName)
	Local $hWnd = ControlGetHandle($ScanSettingsdialog, "Default Scan Options", $iDocumentTypeCombox)
	If $hWnd == '' Then Return False
	If _GUICtrlComboBox_SelectString($hWnd, $sFormat) == '-1' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'SetDocumentType($sFormat)', $TestCaseID, $sFunctionName, ' ')	
		Return False
	EndIf
	ControlClick($ScanSettingsdialog, "Default Scan Options", $iDocumentTypeCombox)
	Sleep(300)
	If $sFormat == 'Others' Then
		Send('{UP}')
		Sleep(200)
		Send('{DOWN}')
	Else
		Send('{DOWN}')
		Sleep(200)
		Send('{UP}')
	EndIf 
	Sleep(200)
	Send('{ENTER}')
	Sleep(300)
	Return True
EndFunc

Func GetBrightnessStatusFromExcel($sExcelSheetName, $iRow,$TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 12
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetBrightnessStatusFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func GetContrastStatusFromExcel($sExcelSheetName, $iRow,$TestCaseID, $sFunctionName)
	_ExcelSheetActivate($oExcel, $sExcelSheetName)	
	If @error Then Return ''
	Local $iColumn = 13
	Local $sType = _ExcelReadCell($oExcel, $iRow , $iColumn)
	If $sType == '' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'GetContrastStatusFromExcel($sExcelSheetName, $iRow)', $TestCaseID, $sFunctionName, ' ')
	EndIf
	Return $sType
EndFunc

Func SetContrast($sColorMode, $sBrightness, $sContrast,$TestCaseID, $sFunctionName)
	If $sColorMode = 'Black and White' Then
		If $sBrightness == 'Maximum' And $sContrast == 'Maximum' Then
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iThresholdEditbox, 255)
			If Not @error Then Return True
		EndIf
		If $sBrightness == 'Maximum' And $sContrast == 'Minimum' Then
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iThresholdEditbox, 125)
			If Not @error Then Return True
		EndIf	
		If $sBrightness == 'Minimum' And $sContrast == 'Maximum' Then
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iThresholdEditbox, 125)
			If Not @error Then Return True
		EndIf	
		If $sBrightness == 'Minimum' And $sContrast == 'Minimum' Then
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iThresholdEditbox, 0)
			If Not @error Then Return True
		EndIf
		If $sBrightness == '0' And $sContrast == '0' Then
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iThresholdEditbox, 0)
			If Not @error Then Return True
		EndIf	
	Else
		If $sBrightness == 'Maximum' And $sContrast == 'Maximum' Then
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iBrightnessEditbox, 100)
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iContrastEditbox, 100)
			If Not @error Then Return True
		EndIf
		If $sBrightness == 'Maximum' And $sContrast == 'Minimum' Then
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iBrightnessEditbox, 100)
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iContrastEditbox, -100)
			If Not @error Then Return True
		EndIf	
		If $sBrightness == 'Minimum' And $sContrast == 'Maximum' Then
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iBrightnessEditbox, -100)
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iContrastEditbox, 100)
			If Not @error Then Return True
		EndIf	
		If $sBrightness == 'Minimum' And $sContrast == 'Minimum' Then
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iBrightnessEditbox, -100)
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iContrastEditbox, -100)
			If Not @error Then Return True
		EndIf
		If $sBrightness == '0' And $sContrast == '0' Then
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iBrightnessEditbox, 0)
			ControlSetText($ScanSettingsdialog, "Default Scan Options", $iContrastEditbox, 0)
			If Not @error Then Return True
		EndIf
	EndIf
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'SetContrast($sColorMode, $sBrightness, $sContrast)', $TestCaseID, $sFunctionName, ' ')
		Return False
EndFunc	
	
Func FileExtension($sFileName,$TestCaseID, $sFunctionName)
	Switch $sFileName
	Case 'TIFF-multiple page(*.tif)'
		Return '.tif'
	Case 'JPEG(*.jpg)'
		Return '.jpg'	
	Case 'TIFF-G3 compression(*.tif)'
		Return '.tif'
	Case 'TIFF-G4 compression(*.tif)'
		Return '.tif'	
	Case 'PNG(*.png)'
		Return '.png'	
	Case 'Photoshop(*.PSD)'
		Return '.PSD'
	Case 'PDF(*.pdf)'
		Return '.pdf'	
	Case 'GIF(*.gif)'
		Return '.gif'
	Case 'Targa(*.tga)'
		Return '.tga'	
	Case 'Bitmap(*.bmp)'
		Return '.bmp'
	Case 'TIFF-uncompressed(*.tif)'
		Return '.tif'	
	Case 'PICT(*.pct)'
		Return '.pct'	
	Case 'PaintBrush(*.pcx)'
		Return '.pcx'			
	EndSwitch
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & 'FileExtension($sFileName)', $TestCaseID, $sFunctionName, ' ')
	Return ''
EndFunc

Func SetColorMode($sFormat,$TestCaseID, $sFunctionName)
	Local $hWnd = ControlGetHandle($ScanSettingsdialog, "Default Scan Options", $iColorModeCombox)
	If $hWnd == '' Then Return False
	If _GUICtrlComboBox_SelectString($hWnd, $sFormat) == '-1' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'SetColorMode($sFormat)', $TestCaseID, $sFunctionName, ' ')	
		Return False
	EndIf
	ControlClick($ScanSettingsdialog, "Default Scan Options", $iColorModeCombox)
	Sleep(300)
	If $sFormat == 'Color' Then
		Send('{UP}')
		Sleep(200)
		Send('{DOWN}')
	Else
		Send('{DOWN}')
		Sleep(200)
		Send('{UP}')
	EndIf 
	Sleep(200)
	Send('{ENTER}')
	Sleep(300)
	Return True
EndFunc

Func SetScanSize($sFormat,$TestCaseID, $sFunctionName)
	
	Local $hWnd = ControlGetHandle($ScanSettingsdialog, "Default Scan Options", $iScanSizeCombox)
	If $hWnd == '' Then Return False
	_GUICtrlComboBox_SelectString($hWnd, $sFormat)
	ControlClick($ScanSettingsdialog, "Default Scan Options", $iScanSizeCombox)
	Sleep(300)
	If $sFormat == 'A5 148 x 210 mm' Then
		Send('{UP}')
		Sleep(200)
		Send('{DOWN}')
	ElseIf $sFormat == 'Custom(Maximum*Minimum)' Then 
		SetCustomScanSize(14, 0.2,$TestCaseID, $sFunctionName)
	ElseIf $sFormat == 'Custom(Minimum*Maximum)' Then 
		SetCustomScanSize(0.2, 8.5,$TestCaseID, $sFunctionName)
	ElseIf $sFormat == 'Custom(Minimum*Minimum)' Then 
		SetCustomScanSize(0.2, 0.2,$TestCaseID, $sFunctionName)
	ElseIf $sFormat == 'Custom(Maximum*Maximum)' Then 
		SetCustomScanSize(14, 8.5,$TestCaseID, $sFunctionName)
	Else
		Send('{DOWN}')
		Sleep(200)
		Send('{UP}')
	EndIf 
	Sleep(200)
	Send('{ENTER}')
	Sleep(300)
	Return True
EndFunc

Func SetCustomScanSize($iCustomHeight, $iCustomWidth,$TestCaseID, $sFunctionName)
	Local $sCusTitle = 'Customize Size', $sCusText = 'Dimensions'
	Send('{UP}')
	Sleep(200)
	Send('{DOWN}')
	If WinWaitActive($sCusTitle, $sCusText, 10) Then
		Sleep(500)
		ControlSetText($sCusTitle, $sCusText, 2068, $iCustomHeight)   ;Height
		If @error Then 
			Common_ClosePMWhenException()
			Common_WriteLog($sFaile & 'SetCustomScanSize($iCustomHeight, $iCustomWidth)', $TestCaseID, $sFunctionName, ' ')	
			Return False
		EndIf
		ControlSetText($sCusTitle, $sCusText, 2065, $iCustomWidth)   ;Width
		If @error Then 
			Common_ClosePMWhenException()
			Common_WriteLog($sFaile & 'SetCustomScanSize($iCustomHeight, $iCustomWidth)', $TestCaseID, $sFunctionName, ' ')	
			Return False
		EndIf
		Sleep(300)
		If Not ControlClick($sCusTitle, $sCusText, 'Button1') Then
			Common_ClosePMWhenException()
			Common_WriteLog($sFaile & 'SetCustomScanSize($iCustomHeight, $iCustomWidth)', $TestCaseID, $sFunctionName, ' ')	
			Return False
		Else
			Return True
		EndIf
	EndIf
EndFunc

Func SetDPI($sFormat,$TestCaseID, $sFunctionName, $Type)
	Local $hWnd = ControlGetHandle($ScanSettingsdialog, "Default Scan Options", $iDPICombox)
	If $hWnd == '' Then Return False
	If _GUICtrlComboBox_SelectString($hWnd, $sFormat) == '-1' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'SetDPI($sFormat)', $TestCaseID, $sFunctionName, ' ')	
		Return False
	EndIf
	ControlClick($ScanSettingsdialog, "Default Scan Options", $iDPICombox)
	Sleep(300)
	If $Type == 'TWAIN' Then
		If $sFormat == '2400 dpi' Then
			Send('{UP}')
			Sleep(200)
			Send('{DOWN}')
		Else
			Send('{DOWN}')
			Sleep(200)
			Send('{UP}')
		EndIf 
		Sleep(200)
		Send('{ENTER}')
		Sleep(300)
	Else
		If $sFormat == '600  dpi' Then
			Send('{UP}')
			Sleep(200)
			Send('{DOWN}')
		Else
			Send('{DOWN}')
			Sleep(200)
			Send('{UP}')
		EndIf 
		Sleep(200)
		Send('{ENTER}')
		Sleep(300)
	EndIf
	Return True
EndFunc

Func SetSaveAsType($sFormat,$TestCaseID, $sFunctionName)
	Local $hWnd = ControlGetHandle($ScanSettingsdialog, "Default Scan Options", $iSaveAsTypeCombox)
	If $hWnd == '' Then Return False
	If _GUICtrlComboBox_SelectString($hWnd, $sFormat) == '-1' Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'SetSaveAsType($sFormat)', $TestCaseID, $sFunctionName, ' ')	
		Return False
	EndIf
	ControlClick($ScanSettingsdialog, "Default Scan Options", $iSaveAsTypeCombox)
	Sleep(300)
	If $sFormat == 'TIFF-multiple page(*.tif)' Then
		Send('{UP}')
		Sleep(200)
		Send('{DOWN}')
	Else
		Send('{DOWN}')
		Sleep(200)
		Send('{UP}')
	EndIf 
	Sleep(200)
	Send('{ENTER}')
	Sleep(300)
	Return True
EndFunc

Func InitializeStatusForScan()
	Local $FileList = _FileListToArray ($sFunctionFolderPath,"*", 1)
	If Not @error Then
		If $FileList[0] >= 1 Then
			For $i = 1 to $FileList[0]
				FileDelete ($sFunctionFolderPath & $FileList[$i])
			Next
			Return 1
		EndIf
	EndIf	
	If FileExists($sFilePatch & $sLongFolderName) Then DirRemove($sFilePatch & $sLongFolderName, 1)
	
EndFunc

Func DeleteFile($sFile, $TestCaseID, $sFunctionName)
	If Not FileDelete($sFile) Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'DeleteFile($sFile, $TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
		Return False
	EndIf	
	Return True	
EndFunc

Func CopyFile($sSrc, $sDes, $TestCaseID, $sFunctionName)
	If Not FileCopy($sSrc, $sDes, 9) Then
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'CopyFile($sSrc, $sDes, $TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
		Return False
	EndIf	
	Return True
EndFunc

Func ClickScanButtonInToolBar($hPMHandle, $ScanButtonInToolbar, $TestCaseID, $sFunctionName)
	ControlClick($hPMHandle, '', $ScanButtonInToolbar)
	If Not @error Then 
		Common_SleepTime(6)
		If WinExists('ScanGear', 'Cancel') Then
			WinActivate('ScanGear', 'Cancel')
			Sleep(300)
			ControlClick('ScanGear', 'Cancel', 'Button3')
			Sleep(300)
			ControlClick('ScanGear', 'Cancel', 'Button1')
			Common_SleepTime(6)
		EndIf
		Return True
	Else
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'ClickScanButtonInToolBar($hPMHandle, $ScanButtonInToolbar, $TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
		Return False
	EndIf
EndFunc

Func IsScannedFileCreated($sPath)
	For $i = 1 to 120
		Common_SleepTime(2)
		If FileExists($sPath) Then
			Common_SleepTime(2)
			Return True
		EndIf
	Next
	Return False
EndFunc

Func WaitScanFinished($sScanProgressBarTitle, $iScanTime, $TestCaseID, $sFunctionName)
	Common_SleepTime(15)
	
	;Close alert message of scanner
	If WinExists('ScanGear','OK') Then
		WinActivate('ScanGear','OK')
		Sleep(300)
		ControlClick('ScanGear','OK', 'Button3')
		Sleep(300)
		ControlClick('ScanGear','OK', 'Button1')
	EndIf
	
	Common_SleepTime(12)
	
	If WinWaitClose($sScanProgressBarTitle, '', $iScanTime) Then
		Common_SleepTime(10)
		If WinExists('[CLASS:#32770]', '&Yes') Then
			WinActivate('[CLASS:#32770]', '&Yes')
			Sleep(300)
			ControlClick('[CLASS:#32770]', '&Yes', 'Button2')
		EndIf
		Return True
	EndIf

	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & 'WaitScanFinished($sScanProgressBarTitle, $iScanTime, $TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
	Return False	
EndFunc

Func WaitScanFinished_WIA($sScanProgressBarTitle, $iScanTime, $TestCaseID, $sFunctionName)
	Common_SleepTime(7)
	
	;Close alert message of scanner
	If WinExists('ScanGear','OK') Then
		WinActivate('ScanGear','OK')
		Sleep(300)
		ControlClick('ScanGear','OK', 'Button3')
		Sleep(300)
		ControlClick('ScanGear','OK', 'Button1')
	EndIf
	Common_SleepTime(12)
	
	If WinWaitClose("[CLASS:#32770]", $sScanningProgressBarDlgTextWIA, $iScanTime) Then
		Common_SleepTime(6)
		If WinExists('Presto! PageManager', '&Yes') Then
			WinActivate('Presto! PageManager', '&Yes')
			Sleep(300)
			ControlClick('Presto! PageManager', '&Yes', 'Button2')
		EndIf
		Return True
	EndIf
	
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & 'WaitScanFinished($sScanProgressBarTitle, $iScanTime, $TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
	Return False	
EndFunc

Func ClickScanButtonOnScnnerShowUI($sScannerShowUIDlgTitle, $sButtonID ,$TestCaseID, $sFunctionName)
	If WinWaitActive($sScannerShowUIDlgTitle, "", 20) Then
		Common_SleepTime(1)
		ControlClick($sScannerShowUIDlgTitle, '', $sButtonID)
		If Not @error Then 
			Common_SleepTime(6)
			Return True
		EndIf	
	EndIf
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & 'ClickScanButtonOnScnnerShowUI($sScannerShowUIDlgTitle, $sButtonID ,$TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
	Return False
EndFunc

Func SelectDriverInSelectSource($sScannerDriverNameInSelectSource,$TestCaseID, $sFunctionName)
	Send("!f")
	Common_SleepTime(1)
	Send("c")
	If WinWaitActive($sSelectSourceDlg, "", 60) Then
		Common_SleepTime(2)
		Local $hDld = WinGetHandle($sSelectSourceDlg, "")
		Local $hCtrl = ControlGetHandle($hDld, '', $sSelectSourceListBox)
		If $hCtrl <> '' Then
			Local $iIndex = _GUICtrlListBox_FindString($hCtrl, $sScannerDriverNameInSelectSource)
			If Not @error Then
				_GUICtrlListBox_ClickItem($hCtrl, $iIndex)
				Sleep(300)
				ControlClick($sSelectSourceDlg, '', 1)  ;OK button
				If Not @error Then Return True
			EndIf
		EndIf
	EndIf
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & 'SelectDriverInSelectSource($sScannerDriverNameInSelectSource,$TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
	Return False
EndFunc

Func LoadScanSettings($sFileName, $TestCaseID, $sFunctionName)
	Local $sTile = 'Open', $sText = 'FolderView'
	If WinWaitActive($sTile, $sText, 10) Then
		ControlSetText($sTile, $sText, 1152, $sFileName)
		If Not @error Then
			Sleep(300)
			ControlClick($sTile, $sText, 'Button2')
			If Not @error Then 
				Common_SleepTime(1)
				Return True
			EndIf
		EndIf
	EndIf
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & 'LoadScanSettings($sFileName, $TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
	Return False
EndFunc

Func SaveScanSettings($sFileName, $TestCaseID, $sFunctionName)
	Local $sTile = 'Save As', $sText = 'FolderView'
	If WinWaitActive($sTile, $sText, 10) Then
		ControlSetText($sTile, $sText, 1152, $sFileName)
		If Not @error Then
			Sleep(300)
			ControlClick($sTile, $sText, 'Button2')
			If Not @error Then 
				Common_SleepTime(1)
				Return True
			EndIf
		EndIf
	EndIf
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & ' SaveScanSettings($sFileName, $TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
	Return False
EndFunc


Func ClickButtonInScanSettingDlg($sButton, $hScanSettingDialog, $TestCaseID, $sFunctionName)
	If $sButton = 'OK' Then
		ControlClick($hScanSettingDialog, '', $iOKButton)
		Common_SleepTime(2)
		If Not @error Then Return True
	EndIf
	If $sButton = 'Scan to My Pagemanager Folder' Then
		ControlClick($hScanSettingDialog, '', $ScanToMyPagemanagerFolderBtnInScanSetting)
		Common_SleepTime(2)
		If Not @error Then Return True
	EndIf		
	If $sButton = 'Scan to Active Folder' Then
		ControlClick($hScanSettingDialog, '', $iScanToActiveFolderBtnInScanSetting)
		Common_SleepTime(2)
		If Not @error Then Return True
	EndIf	
	If $sButton = 'Save Settings' Then
		ControlClick($hScanSettingDialog, '', $iSaveSettingsBtnInInScanSetting)
		Common_SleepTime(2)
		If Not @error Then Return True
	EndIf
	If $sButton = 'Load Settings' Then
		ControlClick($hScanSettingDialog, '', $iLoadSettingsBtnInInScanSetting)
		Common_SleepTime(2)
		If Not @error Then Return True
	EndIf

	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & 'ClickButtonInScanSettingDlg($sButton, $hScanSettingDialog, $TestCaseID, $sScanFunctionName)', $TestCaseID, $sFunctionName, ' ')	
	Return False
EndFunc

Func SelectLongFolder($hPMHandle, $sFolderName, $TestCaseID, $sFunctionName)
	If Common_SwitchToFunctionFolder($hPMHandle, $sFolderName, $TestCaseID, $sFunctionName) Then
		Local $hTree = ControlGetHandle($hPMHandle, "", $sTreeViewCtrInfo)
		Local $hItemFound = _GUICtrlTreeView_FindItem($hTree, $sLongFolderName)
		If Not @error Then
			_GUICtrlTreeView_ClickItem($hTree, $hItemFound)
			If Not @error Then 
				Sleep(300)
				Return True
			EndIf	
		EndIf
	EndIf
	Common_WriteLog($sFaile & 'SelectLongFolder($sPMHandle, $sFolderName, $TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
	Return False			
EndFunc

Func SelectDoubleByteFolder($hPMHandle, $sFolderName, $TestCaseID, $sFunctionName)
	If Common_SwitchToFunctionFolder($hPMHandle, $sFolderName, $TestCaseID, $sFunctionName) Then
		Local $hTree = ControlGetHandle($hPMHandle, "", $sTreeViewCtrInfo)
		Local $hItemFound = _GUICtrlTreeView_FindItem($hTree, 'Sample')
		If Not @error Then
			_GUICtrlTreeView_ClickItem($hTree, $hItemFound)
			If Not @error Then 
				Send("{DOWN}")
				Sleep(300)
				Return True
			EndIf	
		EndIf
	EndIf
	Common_WriteLog($sFaile & 'SelectDoubleByteFolder($sPMHandle, $sFolderName, $TestCaseID, $sFunctionName)', $TestCaseID, $sFunctionName, ' ')
	Return False			
EndFunc


Func SetPrefixValueInScanSettingDlg($sValue, $hScanSettingDialog, $TestCaseID, $sFunctionName)
	ControlSetText($hScanSettingDialog, '', $iPreifxEditBox, $sValue)
	If Not @error Then
		Common_SleepTime(1)
		If ControlGetText($hScanSettingDialog, '', $iPreifxEditBox) == $sValue Then Return True
	EndIf
	ControlSetText($hScanSettingDialog, '', 2057, $sValue)
	If Not @error Then Return True
	Common_ClosePMWhenException()	
	Common_WriteLog($sFaile & ' SetPrefixValueInScanSettingDlg($sValue, $hScanSettingDialog, $TestCaseID, $sScanFunctionName)', $TestCaseID, $sFunctionName, ' ')
	Return False	
EndFunc

Func NonUIScanCheckBoxInScanSettingDlg($sStatues, $hScanSettingDialog, $TestCaseID, $sFunctionName)
	If $sStatues == 'Checkin' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iNonUICheckBox))) == 0 Then  
			ControlClick($hScanSettingDialog,'',$iNonUICheckBox)
			If Not @error Then
				Common_SleepTime(1)
				Return True
			EndIf
		Else
			Common_SleepTime(1)
			Return True
		EndIf
	ElseIf $sStatues == 'Uncheck' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iNonUICheckBox))) == 1 Then  
			ControlClick($hScanSettingDialog,'',$iNonUICheckBox)
			If Not @error Then
				Common_SleepTime(1)
				Return True
			EndIf
		Else
			Common_SleepTime(1)
			Return True
		EndIf
	EndIf
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & ' NonUIScanCheckBoxInScanSettingDlg($sStatues, $hScanSettingDialog, $TestCaseID, $sScanFunctionName)', $TestCaseID, $sFunctionName, ' ')
EndFunc

Func EnableDocumentFeederCheckBoxInScanSettingDlg($sStatues, $hScanSettingDialog, $TestCaseID, $sFunctionName)
	If $sStatues == 'On' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iEnableDocumentFeederCheckbox))) == 0 Then  
			ControlClick($hScanSettingDialog,'',$iEnableDocumentFeederCheckbox)
			If Not @error Then
				Common_SleepTime(1)
				Return True
			EndIf
		Else
			Common_SleepTime(1)
			Return True
		EndIf
	ElseIf $sStatues == 'Off' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iEnableDocumentFeederCheckbox))) == 1 Then  
			ControlClick($hScanSettingDialog,'',$iEnableDocumentFeederCheckbox)
			If Not @error Then
				Common_SleepTime(1)
				Return True
			EndIf
		Else
			Common_SleepTime(1)
			Return True
		EndIf
	EndIf
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & ' EnableDocumentFeederCheckBoxInScanSettingDlg($sStatues, $hScanSettingDialog, $TestCaseID, $sScanFunctionName)', $TestCaseID, $sFunctionName, ' ')
EndFunc

Func ContinueScanCheckBoxInScanSettingDlg($sStatues, $hScanSettingDialog, $TestCaseID, $sFunctionName)
	If $sStatues == 'On' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iContinueScanCheckbox))) == 0 Then  
			ControlClick($hScanSettingDialog,'',$iContinueScanCheckbox)
			If Not @error Then
				Common_SleepTime(1)
				Return True
			EndIf
		Else
			Common_SleepTime(1)
			Return True
		EndIf
	ElseIf $sStatues == 'Off' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iContinueScanCheckbox))) == 1 Then  
			ControlClick($hScanSettingDialog,'',$iContinueScanCheckbox)
			If Not @error Then
				Common_SleepTime(1)
				Return True
			EndIf
		Else
			Common_SleepTime(1)
			Return True
		EndIf
	EndIf
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & ' ContinueScanCheckBoxInScanSettingDlg($sStatues, $hScanSettingDialog, $TestCaseID, $sScanFunctionName)', $TestCaseID, $sFunctionName, ' ')
EndFunc

Func AutoCroppingCheckboxCheckBox($sStatues, $hScanSettingDialog, $TestCaseID, $sFunctionName)
	If $sStatues == 'Bizcard' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iAutocropCheckbox))) == 0 Then  
			ControlClick($hScanSettingDialog,'',$iAutocropCheckbox)
			If Not @error Then
				Common_SleepTime(1)
				Return True
			EndIf
		Else
			Common_SleepTime(1)
			Return True
		EndIf
	ElseIf 	$sStatues == 'Picture' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iAutocropCheckbox))) == 0 Then  
			ControlClick($hScanSettingDialog,'',$iAutocropCheckbox)
			If Not @error Then
				Common_SleepTime(1)
				ControlClick($hScanSettingDialog,'',$iAutocropCombox)
				If Not @error Then
					Common_SleepTime(1)				
					Send('{DOWN}')
					Sleep(200)
					Send('{ENTER}')
					Sleep(200)
					Return True
				EndIf
			EndIf
		EndIf
	ElseIf $sStatues == 'Off' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iAutocropCheckbox))) == 1 Then  
			ControlClick($hScanSettingDialog,'',$iAutocropCheckbox)
			If Not @error Then
				Common_SleepTime(1)
				Return True
			EndIf
		Else
			Common_SleepTime(1)
			Return True
		EndIf
	EndIf
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & ' AutoCroppingCheckboxCheckBox($sStatues, $hScanSettingDialog, $TestCaseID, $sScanFunctionName)', $TestCaseID, $sFunctionName, ' ')
EndFunc


Func EnableDDescreenCheckboxCheckBoxInScanSettingDlg($sStatues, $hScanSettingDialog, $TestCaseID, $sFunctionName)
	If $sStatues == 'On' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iDescreenCheckbox))) == 0 Then  
			ControlClick($hScanSettingDialog,'',$iDescreenCheckbox)
			If Not @error Then
				Common_SleepTime(1)
				Return True
			EndIf
		Else
			Common_SleepTime(1)
			Return True
		EndIf
	ElseIf $sStatues == 'Off' Then
		If (_GUICtrlButton_GetCheck(ControlGetHandle($hScanSettingDialog,'',$iDescreenCheckbox))) == 1 Then  
			ControlClick($hScanSettingDialog,'',$iDescreenCheckbox)
			If Not @error Then
				Common_SleepTime(1)
				Return True
			EndIf
		Else
			Common_SleepTime(1)
			Return True
		EndIf
	EndIf
	Common_ClosePMWhenException()
	Common_WriteLog($sFaile & ' EnableDDescreenCheckboxCheckBoxInScanSettingDlg($sStatues, $hScanSettingDialog, $TestCaseID, $sScanFunctionName)', $TestCaseID, $sFunctionName, ' ')
EndFunc

Func OpenScanSettingsDialogFromFileMenu($TestCaseID, $sFunctionName)
	Send("!f")
	Common_SleepTime(1)
	Send("a")
	If WinWaitActive($ScanSettingsdialog, "Default Scan Options", 60) Then
		Common_SleepTime(2)
		Return WinGetHandle($ScanSettingsdialog, "Default Scan Options")
	Else
		Common_ClosePMWhenException()
		Common_WriteLog($sFaile & 'OpenScanSettinsDialogFromFileMenu($TestCaseID, $sScanFunctionName)', $TestCaseID, $sFunctionName, ' ')
		Return ''
	EndIf
EndFunc

Func ScanUIMode($ScanSettingsdialog, $state)
	$hWnd = ControlGetHandle($ScanSettingsdialog, "", 2012)
	$checked = _GUICtrlButton_GetCheck($hWnd)
	Switch $state
	Case "NonUI"
		If $checked = 1 Then
		Else
			ControlClick($ScanSettingsdialog, "", 2012)
			Sleep(500)
		EndIf
	Case "ShowUI"
		If $checked = 1 Then
			ControlClick($ScanSettingsdialog, "", 2012)
			Sleep(500)			
		EndIf		
	EndSwitch
EndFunc

Func ScanFrom($ScanSettingsdialog, $state)
	$hWnd = ControlGetHandle($ScanSettingsdialog, "", 2013)
	$checked = _GUICtrlButton_GetCheck($hWnd)
	Switch $state
	Case "ADF"
		If $checked = 1 Then
		Else
			ControlClick($ScanSettingsdialog, "", 2013)
			Sleep(500)
		EndIf
	Case "Flatbed"
		If $checked = 1 Then
			ControlClick($ScanSettingsdialog, "", 2013)
			Sleep(500)			
		EndIf		
	EndSwitch
EndFunc		
	
Func ClickOKButton($ScanSettingsdialog)	
	ControlClick($ScanSettingsdialog, "", 1)
	Sleep(1000)
EndFunc

Func SetPrefix($ScanSettingsdialog, $Prefix)
	ControlFocus($ScanSettingsdialog, "", 2057)
	For $i = 0 to 25
		Send("{DEL}")
	Next
	ControlSend($ScanSettingsdialog, "", 2057, $Prefix)
	Sleep(1000)
EndFunc

Func ClickScanButton($PMTitle)	
	Local $title = "Scanning..."
	ControlClick($PMTitle, "", 1003)
	Sleep(1500)
	WinWaitClose($title, "Scanner is warming up..." & @LF & "Do not open the document cover.", 50)
	Sleep(1500)
	WinWaitClose($title, "Cancel", 450)
	Sleep(5000)
EndFunc	

Func GetImageType($Path)
	If FileExists($Path) Then
		$dllb = DllOpen ($Dll3)
		$dllc = DllOpen ($Dll4)
		$dlla = DllOpen ($Dll1)
		$dll = DllOpen ($Dll2)
		$result = DllCall($dll, "int","Auto_GetImageType", "str",$Path)
		Return $result[0]
		DllClose($dll)
		DllClose($dlla)
		DllClose($dllb)
		DllClose($dllc)	
	EndIf
EndFunc		

	
Func GetJPGFileDPI($Path)
	$hFile = FileOpen($Path, 16)
	$bData = FileRead($hFile, 1024)
	FileClose($hFile)
	$tagJPEG = "byte Header[6];char JFIF[5];byte Version[2];byte DensityUnits;byte XPixel[2];byte YPixel[2];ubyte XZoomPixel;ubyte YZooomPixel;byte Signature[2]"
	$tBinary = DllStructCreate("byte Binary[1024]")
	$pBuffer = DllStructGetPtr($tBinary)
	$tBuffer = DllStructCreate($tagJPEG, $pBuffer)
	DllStructSetData($tBinary, "Binary", $bData)
	$tBuffer1 = $tBuffer
	If (DllStructGetData($tBuffer, "Signature") = "0xFFDB") Then
		$pBuffer += DllStructGetSize($tBuffer) + 69 + 67
	Else
		$pBuffer += DllStructGetSize($tBuffer) - 2
	EndIf
	$tBuffer = DllStructCreate("byte Signature[2]", $pBuffer)
	If (DllStructGetData($tBuffer, "Signature") = "0xFFFE") Then
		$pBuffer += DllStructGetSize($tBuffer)
		$tBuffer = DllStructCreate("byte CommentBytes[2]", $pBuffer)
		$pBuffer += Number(String(DllStructGetData($tBuffer, "CommentBytes")))
	Else
		$pBuffer += DllStructGetSize($tBuffer) - 2
	EndIf
	$tBuffer = DllStructCreate("byte Signature[2]", $pBuffer)
	Switch DllStructGetData($tBuffer, "Signature")
	Case ("0xFFC0")
		$pBuffer += 4
	Case ("0xFFDB")
		$pBuffer += 67 * 2 + 8
	EndSwitch
	$tBuffer = DllStructCreate("byte Accuracy;byte Height[2];byte Width[2]", $pBuffer)
	$xDPI = Number(String(DllStructGetData($tBuffer1, "XPixel")))   
	$yDPI = Number(String(DllStructGetData($tBuffer1, "YPixel")))
	$width = Number(String(DllStructGetData($tBuffer, "Width")))
	$height = Number(String(DllStructGetData($tBuffer, "Height")))
	Number(String(DllStructGetData($tBuffer, "Accuracy"))) ;  "Accuracy: "
	Local $avArray[10]
	$avArray[0] = $xDPI
	$avArray[1] = $yDPI
	$avArray[2] = $width
	$avArray[3] = $height
	Return $avArray
 EndFunc
	
Func ScanWIthShowUI($PMTitle)	
	ControlClick($PMTitle, "", 1003)
	Sleep(1500)
	WinWaitActive("ScanGear", "Select Source", 10)
	Sleep(1000)
	ControlClick("ScanGear", "", 7151)   ; Click Scan button of show UI.
	Sleep(1500)
	WinWaitClose("Scanning...", "Scanner is warming up..." & @LF & "Do not open the document cover.", 50)
	Sleep(1500)
	WinWaitClose("Scanning...", "Cancel", 50)
	Sleep(10000)	
 EndFunc	

Func SetScanMode($ScanSettingsdialog, $Type)
	$hWnd = ControlGetHandle($ScanSettingsdialog, "", 2041)
	_GUICtrlComboBox_SelectString($hWnd, $Type)
	Sleep(500)
	ControlClick($ScanSettingsdialog, "", 2041)
	Sleep(500)
	If $Type = "Black and White" Then
		Send("{DOWN}")
		Sleep(300)
		Send("{UP}")
		Sleep(300)
		Send("{ENTER}")
		Sleep(500)
	EndIf
	If $Type = "Gray" Then
		Send("{DOWN}")
		Sleep(300)
		Send("{UP}")
		Sleep(300)
		Send("{ENTER}")
		Sleep(500)
	EndIf	
	If $Type = "Color" Then
		Send("{UP}")
		Sleep(300)
		Send("{DOWN}")
		Sleep(300)
		Send("{ENTER}")
		Sleep(500)
	EndIf
EndFunc

Func BrightnessNONUI($ScanSettingsdialog, $Value)
	ControlFocus($ScanSettingsdialog, "", 2049)
	ControlSetText($ScanSettingsdialog, "", 2049, $Value)
	Sleep(500)
EndFunc

Func ContrastNONUI($ScanSettingsdialog, $Value)
	ControlFocus($ScanSettingsdialog, "", 2050)
	ControlSetText($ScanSettingsdialog, "", 2050, $Value)
	Sleep(500)
EndFunc

Func ScanProfile($ScanSettingsdialog)
	ControlClick($ScanSettingsdialog, "", 2072)
	Sleep(1500)
EndFunc	

Func SetProfileName($profile,$name)
	ControlFocus($profile, "", 1152)
	ControlSetText($profile, "", 1152,$name)
	Sleep(500)
EndFunc	

Func ClickSaveButton($profile)
	ControlClick($profile, "", "Button2")   ; Click Save button
	Sleep(1500)
EndFunc

Func LoadScanProfile($ScanSettingsdialog)
	ControlClick($ScanSettingsdialog, "", 2076)   ; Click Save button
	Sleep(1500)
EndFunc

Func SetLoadScanProfileName($open,$name)
	ControlFocus($open, "", 1152)
	ControlSetText($open, "", 1152,$name)
	Sleep(500)
	ControlClick($open, "", "Button2")
	Sleep(1500)
EndFunc

Func ClosePMSBErrorDialog()
	If WinExists("Prestopm.exe") Then
		WinClose("Prestopm.exe")
	EndIf
	If WinExists("Pmsb.exe") Then
		WinClose("Pmsb.exe")
	EndIf
EndFunc

Func AutocroppingOff($ScanSettingsdialog)
	Local $h = ControlGetHandle($ScanSettingsdialog, "", 2060)
	$v = _GUICtrlButton_GetCheck($h)
	If $v = 1 Then
		ControlClick($ScanSettingsdialog, "", 2060)
		Sleep(1000)
	EndIf	
EndFunc

Func AutocroppingOn($ScanSettingsdialog, $Style)
	Local $h = ControlGetHandle($ScanSettingsdialog, "", 2060)
	$v = _GUICtrlButton_GetCheck($h)
	If $v = 0 Then
		ControlClick($ScanSettingsdialog, "", 2060)
		Sleep(1000)
	EndIf	
	Local $hcom = ControlGetHandle($ScanSettingsdialog, "", 2035)
	If $Style = "Picture" Then
		_GUICtrlComboBox_SelectString($hcom, "Picture")
	EndIf
	If $Style = "BizCard" Then
		_GUICtrlComboBox_SelectString($hcom, "BizCard")
	EndIf	
EndFunc

Func Decreen($ScanSettingsdialog, $Status)
	If $Status = "Check" Then
		Local $h = ControlGetHandle($ScanSettingsdialog, "", 2032)
		$v = _GUICtrlButton_GetCheck($h)
		If $v = 0 Then
			ControlClick($ScanSettingsdialog, "", 2032)
			Sleep(1000)
		EndIf	
	EndIf
	If $Status = "Uncheck" Then
		Local $h = ControlGetHandle($ScanSettingsdialog, "", 2032)
		$v = _GUICtrlButton_GetCheck($h)
		If $v = 1 Then
			ControlClick($ScanSettingsdialog, "", 2032)
			Sleep(1000)
		EndIf	
	EndIf	
EndFunc	

Func ContinuousScan($ScanSettingsdialog, $Status)
	If $Status = "Check" Then
		Local $h = ControlGetHandle($ScanSettingsdialog, "", 2100)
		$v = _GUICtrlButton_GetCheck($h)
		If $v = 0 Then
			ControlClick($ScanSettingsdialog, "", 2100)
			Sleep(1000)
		EndIf	
	EndIf
	If $Status = "Uncheck" Then
		Local $h = ControlGetHandle($ScanSettingsdialog, "", 2100)
		$v = _GUICtrlButton_GetCheck($h)
		If $v = 1 Then
			ControlClick($ScanSettingsdialog, "", 2100)
			Sleep(1000)
		EndIf	
	EndIf
EndFunc

Func ThresholdNONUI($ScanSettingsdialog, $Value)
	ControlFocus($ScanSettingsdialog, "", 2051)
	ControlSetText($ScanSettingsdialog, "", 2051, $Value)
	Sleep(500)
EndFunc

Func SelectSource()
	Send("!f")
	Sleep(500)
	Send("c")
	;Sleep(500)
	;Send("{ENTER}")
	Sleep(1000)
	;WinWaitActive($ScanStingTitle, "", 10)
EndFunc	

Func SelectScaner($SelectSorceDialogTitle,$Source)
	WinActivate($SelectSorceDialogTitle)
	$hWnd = ControlGetHandle($SelectSorceDialogTitle, "", 1000)
	_GUICtrlListBox_SelectString($hWnd, $Source)
	Sleep(500)
	ControlClick($SelectSorceDialogTitle, "", 1)
EndFunc

Func ClickScanButtonWIA($PMTitle)	
	ControlClick($PMTitle, "", 1003)
	Sleep(5000)
	WinWaitClose("[CLASS:#32770]", "Scanning in progress", 450)
	Sleep(5000)
EndFunc	

Func TaskBar($Status)
	MouseMove(652, 772)
	Sleep(300)
	MouseClick("right", 696, 753)
	Sleep(300)
	Send("r")
	Sleep(800)
	$autoHide = _GUICtrlButton_GetCheck(ControlGetHandle("Taskbar and Start Menu Properties", "", 1102))
	$shown = _GUICtrlButton_GetCheck(ControlGetHandle("Taskbar and Start Menu Properties", "", 1101))
	If $Status = "show" Then
		If $autoHide = 1 Then
			ControlClick("Taskbar and Start Menu Properties", "", 1102)
		EndIf
		If $shown = 0 Then
			ControlClick("Taskbar and Start Menu Properties", "", 1101) 
		EndIf
		ControlClick("Taskbar and Start Menu Properties", "", 1)
		Sleep(300)
		MouseClick("left", 204, 159)
		Sleep(500)
	EndIf
	If $Status = "notShow" Then
		If $autoHide = 0 Then
			ControlClick("Taskbar and Start Menu Properties", "", 1102)
		EndIf
		If $shown = 1 Then
			ControlClick("Taskbar and Start Menu Properties", "", 1101) 
		EndIf
		ControlClick("Taskbar and Start Menu Properties", "", 1)
		Sleep(300)
		MouseClick("left", 204, 159)
		Sleep(500)
	EndIf	
EndFunc

























